package com.telrob.bytevm.vm;

import java.io.UnsupportedEncodingException;

import com.telrob.bytevm.utils.ToolUtils;

/***
 * 封装获取参数
 * @author wh
 *
 */
public class Param {
	private Register R;
	private byte[]data=null;
	public Param(Register R,byte[]code) {
		this.R=R;
		this.data=code;
	}
	/***
	 * 参数的个数
	 * @param R
	 * @return
	 */
	public int getParamSize() {
		return R.AX;
	}
	
	public int getIntParam(int index) {
		int temp=R.SS+R.SP-4-index*4;
		int ax=ToolUtils.getInt(data, temp);
		return ax;
	}
	
	public char getCharParam(int index) {
		int ax=ToolUtils.getInt(data, R.SS+R.SP-4-index*4);
		char c=(char)(ax&0xFF);
		return c;
	}
	
	/**
	 * 获取字符串参数
	 * @param index 下标从0开始
	 * @return
	 */
	public String getStringParam(int index) {
		//获取堆栈中的数据
		int ax=ToolUtils.getInt(data, R.SS+R.SP-4+index);
		int temp=ax;
		while(data[temp]!=0) {
			temp++;
		}
		byte[]bb=new byte[temp-ax];
		for(int i=ax;i<temp;i++) {
			bb[i-ax]=data[i];
		}
		
		String str=null;
		try {
			str = new String(bb,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	@Override
	public String toString() {
		return  R.toString();
	}
	
	
}
